<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="患者姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入医生编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="患者来源" prop="subId">
        <el-cascader
          style="width: 230px"
          :props="defaultProps"
          @change="handleSubChange"
          clearable
          :options="deptOptions"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="添加时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 230px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gonitis:patient:evaluation:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gonitis:patient:evaluation:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gonitis:patient:evaluation:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gonitis:patient:evaluation:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="evaluationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" v-if="true" />
      <el-table-column
        label="患者编号"
        :show-overflow-tooltip="true"
        align="center"
        prop="patientId"
      />
      <el-table-column label="患者姓名" align="center" prop="name" />
      <el-table-column
        label="患者电话"
        :show-overflow-tooltip="true"
        align="center"
        prop="phoneNumber"
      />
      <el-table-column
        label="患者来源"
        :show-overflow-tooltip="true"
        align="center"
        prop="subName"
      />
      <el-table-column label="医生编号" align="center" prop="doctorId" />
      <el-table-column label="医生姓名" align="center" prop="doctorName">
        <template slot-scope="scope">
          <span>{{ scope.row.doctorName ? scope.row.doctorName : "暂无" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加次数" align="center" prop="addCount" />
      <el-table-column label="添加时间" width="100" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        width="150"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gonitis:patient:evaluation:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gonitis:patient:evaluation:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row.id)"
            v-hasPermi="['gonitis:patient:evaluation:detail']"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改专科查体对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="45%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>选择患者</span>
          </div>
          <div class="text item">
            <el-form-item label="基础信息" prop="patientId" v-if="!form.id">
              <el-cascader
                style="width: 230px"
                :props="defaultProps"
                v-model="formSub"
                @change="formSubChange"
                :options="subOptions"
              ></el-cascader>
              <el-select
                style="width: 230px"
                v-model="form.patientId"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in patientOptions"
                  :key="item.id"
                  :label="'[姓名] ' + item.name + '  [电话] ' + item.phone"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="关节侧别" prop="bodySide">
              <el-radio-group v-model="form.bodySide">
                <el-radio
                  border
                  v-for="dict in dict.type.patient_info_body_side"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </div>
        </el-card>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>1、疼痛性质【VAS评分 0-10分】</span>
          </div>
          <div class="text item">
            <el-form-item label="静息痛" prop="restPain">
              <el-input-number v-model="form.restPain" :min="1" :max="10" />
            </el-form-item>
            <el-form-item label="运动痛" prop="movePain">
              <el-input-number v-model="form.movePain" :min="1" :max="10" />
            </el-form-item>
            <el-form-item label="天气变化时疼痛" prop="weatherPain">
              <el-input-number v-model="form.weatherPain" :min="1" :max="10" />
            </el-form-item>
            <el-form-item label="关节畏寒" prop="jointCold">
              <el-input-number v-model="form.jointCold" :min="1" :max="10" />
            </el-form-item>
          </div>
        </el-card>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>2、炎症【VAS评分 0-10分】</span>
          </div>
          <div class="text item">
            <el-form-item label="关节肿胀程度" prop="jointSwell">
              <el-input-number v-model="form.jointSwell" :min="1" :max="10" />
            </el-form-item>
            <el-form-item label="局部皮温" prop="skinTemperature">
              <el-input-number v-model="form.skinTemperature" :min="1" :max="10" />
            </el-form-item>
            <el-form-item label="晨僵时间" prop="morningStiffnessTime">
              <el-input-number v-model="form.morningStiffnessTime" :min="1" :max="10" />
            </el-form-item>
          </div>
        </el-card>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>3、功能【VAS评分 0-10分】</span>
          </div>
          <div class="text item">
            <el-form-item label="蹲起" prop="squating">
              <el-input-number v-model="form.squating" :min="1" :max="10" />
            </el-form-item>
            <el-form-item label="上下楼梯" prop="climbingStairs">
              <el-input-number v-model="form.climbingStairs" :min="1" :max="10" />
            </el-form-item>
            <el-form-item label="一次性站立时间" prop="standingTime">
              <el-input-number v-model="form.standingTime" :min="1" :max="10" />
            </el-form-item>
            <el-form-item label="一次性步行距离" prop="walkingDistance">
              <el-input-number v-model="form.walkingDistance" :min="1" :max="10" />
            </el-form-item>
            <el-form-item label="需要助行器械" prop="walkingAids">
              <el-input-number v-model="form.walkingAids" :min="1" :max="10" />
            </el-form-item>
            <el-form-item label="做家务" prop="doingChores">
              <el-input-number v-model="form.doingChores" :min="1" :max="10" />
            </el-form-item>
          </div>
        </el-card>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>4、其他【VAS评分 0-10分】</span>
          </div>
          <div class="text item">
            <el-form-item label="大腿周径[cm]" prop="thighCircu">
              <el-input
                style="width: 200px"
                v-model="form.thighCircu"
                placeholder="请输入大腿周径"
              />
            </el-form-item>
            <el-form-item label="膝关节活动度" prop="kneeMotion">
              <el-input-number v-model="form.kneeMotion" :min="1" :max="10" />
            </el-form-item>
            <el-form-item label="膝关节ROM测试" prop="jointTesting">
              <el-input-number v-model="form.jointTesting" :min="1" :max="10" />
            </el-form-item>
            <el-form-item label="内侧胫股关节间隙压痛" prop="medialTibia">
              <el-radio-group v-model="form.medialTibia">
                <el-radio
                  border
                  v-for="dict in dict.type.patient_evaluation_exit"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
            <el-form-item label="外侧胫股关节间隙压痛" prop="medicalFibula">
              <el-radio-group v-model="form.medicalFibula">
                <el-radio
                  border
                  v-for="dict in dict.type.patient_evaluation_exit"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
            <el-form-item label="麦氏征" prop="maishiSymptoms">
              <el-radio-group v-model="form.maishiSymptoms">
                <el-radio
                  border
                  v-for="dict in dict.type.patient_evaluation_yin_yang"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
            <el-form-item label="过伸实验" prop="overbendingTest">
              <el-radio-group v-model="form.overbendingTest">
                <el-radio
                  border
                  v-for="dict in dict.type.patient_evaluation_yin_yang"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
            <el-form-item label="过屈试验" prop="hyperextensionTest">
              <el-radio-group v-model="form.hyperextensionTest">
                <el-radio
                  border
                  v-for="dict in dict.type.patient_evaluation_yin_yang"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
            <el-form-item label="Beighton评分" prop="beightonScore">
              <el-input-number v-model="form.beightonScore" :min="1" :max="10" />
            </el-form-item>
          </div>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <evaluation-detail v-if="evaluationVisible" ref="evaluation"></evaluation-detail>
  </div>
</template>

<script>
import {
  listEvaluation,
  getEvaluation,
  delEvaluation,
  addEvaluation,
  updateEvaluation,
} from "@/api/gonitis/evaluation";
import { subTree } from "@/api/gonitis/illSub";
import { listBySubId } from "@/api/gonitis/patient";
import EvaluationDetail from "./evaluationDetail";
export default {
  components: {
    EvaluationDetail,
  },
  name: "Evaluation",
  dicts: [
    "patient_info_body_side",
    "patient_evaluation_exit",
    "patient_evaluation_yin_yang",
  ],
  data() {
    return {
      evaluationVisible: false,
      // 患者列表
      patientOptions: [],
      formSub: undefined,
      // 病情列表
      deptOptions: [],
      subOptions: [],
      // 病情多选框映射
      defaultProps: {
        children: "children",
        label: "label",
        value: "id",
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专科查体表格数据
      evaluationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        subId: undefined,
        name: undefined,
      },
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        patientId: [{ required: true, message: "患者编号不能为空", trigger: "change" }],
        restPain: [{ required: true, message: "静息痛不能为空", trigger: "change" }],
        movePain: [{ required: true, message: "运动痛不能为空", trigger: "change" }],
        weatherPain: [
          {
            required: true,
            message: "天气变化时疼痛不能为空",
            trigger: "change",
          },
        ],
        jointCold: [{ required: true, message: "关节畏寒不能为空", trigger: "change" }],
        jointSwell: [
          { required: true, message: "关节肿胀程度不能为空", trigger: "change" },
        ],
        skinTemperature: [
          { required: true, message: "局部皮温不能为空", trigger: "change" },
        ],
        morningStiffnessTime: [
          { required: true, message: "晨僵时间不能为空", trigger: "change" },
        ],
        squating: [{ required: true, message: "蹲起不能为空", trigger: "change" }],
        climbingStairs: [
          { required: true, message: "上下楼梯不能为空", trigger: "change" },
        ],
        standingTime: [
          {
            required: true,
            message: "一次性站立时间不能为空",
            trigger: "change",
          },
        ],
        walkingDistance: [
          {
            required: true,
            message: "一次性步行距离不能为空",
            trigger: "change",
          },
        ],
        walkingAids: [
          { required: true, message: "需要助行器械不能为空", trigger: "change" },
        ],
        doingChores: [{ required: true, message: "做家务不能为空", trigger: "change" }],
        thighCircu: [{ required: true, message: "大腿周径不能为空", trigger: "blur" }],
        kneeMotion: [
          { required: true, message: "膝关节活动度不能为空", trigger: "change" },
        ],
        jointTesting: [
          {
            required: true,
            message: "膝关节ROM测试不能为空",
            trigger: "change",
          },
        ],
        medialTibia: [
          {
            required: true,
            message: "内侧胫股关节间隙压痛不能为空",
            trigger: "change",
          },
        ],
        medicalFibula: [
          {
            required: true,
            message: "外侧胫股关节间隙压痛不能为空",
            trigger: "change",
          },
        ],
        maishiSymptoms: [
          { required: true, message: "麦氏征不能为空", trigger: "change" },
        ],
        overbendingTest: [
          { required: true, message: "过申实验不能为空", trigger: "change" },
        ],
        hyperextensionTest: [
          { required: true, message: "过屈试验不能为空", trigger: "change" },
        ],
        beightonScore: [
          { required: true, message: "Beighton评分不能为空", trigger: "change" },
        ],
        bodySide: [
          {
            required: true,
            message: "关节侧别[0：左、1：右]不能为空",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.init();
  },
  methods: {
    handleDetail(value) {
      // 发送请求拿到详情
      this.evaluationVisible = true;
      this.$nextTick(() => {
        // 调用子组件方法
        this.$refs.evaluation.init(value);
      });
    },
    /** 病情分类选择框改变 */
    formSubChange(value) {
      this.form.subId = value[value.length - 1];
      // 发送请求查询对应病情的病人
      listBySubId(this.form.subId).then((response) => {
        this.patientOptions = response.data;
      });
    },
    /** 初始化 */
    init() {
      this.getList();
      this.getDeptTree();
    },
    /** 病情分类选择框改变 */
    handleSubChange(value) {
      this.queryParams.subId = value[value.length - 1];
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      subTree().then((response) => {
        this.deptOptions = response.data;
        this.subOptions = response.data;
      });
    },
    /** 查询专科查体列表 */
    getList() {
      this.loading = true;
      listEvaluation(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.evaluationList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        patientId: undefined,
        subId: undefined,
        restPain: undefined,
        movePain: undefined,
        weatherPain: undefined,
        jointCold: undefined,
        jointSwell: undefined,
        skinTemperature: undefined,
        morningStiffnessTime: undefined,
        squating: undefined,
        climbingStairs: undefined,
        standingTime: undefined,
        walkingDistance: undefined,
        walkingAids: undefined,
        doingChores: undefined,
        thighCircu: undefined,
        kneeMotion: undefined,
        jointTesting: undefined,
        medialTibia: undefined,
        medicalFibula: undefined,
        maishiSymptoms: undefined,
        overbendingTest: undefined,
        hyperextensionTest: undefined,
        beightonScore: undefined,
        bodySide: undefined,
      };
      this.formSub = [];
      this.patientOptions = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加专科查体";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getEvaluation(id).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.setForm();
        this.open = true;
        this.title = "修改专科查体";
      });
    },
    setForm() {
      this.form.createTime = null;
      this.form.patientId = null;
      this.form.doctorId = null;
      this.form.addCount = null;
      this.form.subId = null;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateEvaluation(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addEvaluation(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除专科查体编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delEvaluation(ids);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "gonitis/evaluation/export",
        {
          ...this.queryParams,
        },
        `evaluation_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
